/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.xuexiang.xuidemo.util;

import ohos.agp.components.Component;
import ohos.agp.components.ListContainer;
import ohos.multimodalinput.event.TouchEvent;

import com.xuexiang.xuidemo.item.SampleItem;

import java.util.List;

/**
 * xinglu
 *
 * @since 2021-04-28
 */
public class PagerSnapHelper {
    private static final int DIRECTION_UP = 1;
    private static final int DIRECTION_RIGHT = 4;
    private static final int DIRECTION_DOWN = 2;
    private static final int DIRECTION_LEFT = 3;
    private ListContainer mListContainer;
    private List<SampleItem> list;

    // ListContainer滑动方向
    private int moveDirection = 0;

    // 当前被选中的item position
    private int currentPosition = 0;

    /**
     * 绑定listContainer
     *
     * @param listContainer
     * @param listData
     */
    public void attachToRecyclerView(ListContainer listContainer, List<SampleItem> listData) {
        this.mListContainer = listContainer;
        this.list = listData;
        initListContainerListener();
    }

    private void initListContainerListener() {
        mListContainer.setCanAcceptScrollListener(new Component.CanAcceptScrollListener() {
            @Override
            public boolean canAcceptScroll(Component component, int direction, boolean b) {
                // i= 1 向上滑动  2 向下滑动  return true可以滑动   false 不可以滑动
                // 3往左边滑动  4往右边滑动
                moveDirection = direction;
                return true;
            }
        });

        mListContainer.setTouchEventListener(new Component.TouchEventListener() {
            @Override
            public boolean onTouchEvent(Component component, TouchEvent touchEvent) {
                // 有时候监听不到 ACTION_UP   也监听不到 CANCLE  试了其它状态  监听不到antion_up的时候 其它状态也监听不到
                switch (touchEvent.getAction()) {
                    case TouchEvent.PRIMARY_POINT_DOWN:
                        int centerFocusablePosition = mListContainer.getCenterFocusablePosition();
                        currentPosition = centerFocusablePosition;
                        break;
                    case TouchEvent.POINT_MOVE:
                        break;
                    case TouchEvent.PRIMARY_POINT_UP:
                        if (moveDirection == DIRECTION_UP || moveDirection == DIRECTION_LEFT) {
                            if (currentPosition + 1 >= list.size() - 1) {
                                mListContainer.scrollTo(list.size() - 1);
                            } else {
                                mListContainer.scrollTo(currentPosition + 1);
                            }
                        } else if (moveDirection == DIRECTION_DOWN || moveDirection == DIRECTION_RIGHT) {
                            if (currentPosition - 1 <= 0) {
                                mListContainer.scrollTo(0);
                            } else {
                                mListContainer.scrollTo(currentPosition - 1);
                            }
                        }
                        break;
                    case TouchEvent.CANCEL:
                        break;
                    default:
                        break;
                }
                return true;
            }
        });

    }


    public void setData(List<SampleItem> listData) {
        this.list = listData;
    }
}
